package br.com.cast.scc.pages.veiculo;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.cast.scc.enuns.StatusRegistroAtivo;
import br.com.cast.scc.filtros.FiltroPesquisaVeiculos;
import br.com.cast.scc.model.DominioTipoVeiculo;
import br.com.cast.scc.service.TipoVeiculoService;
import br.com.cast.scc.service.VeiculoService;

public class ConsultaVeiculosIndexForm extends Form<FiltroPesquisaVeiculos> {

	private static final long serialVersionUID = 1L;
	
	@SpringBean(name = "tipoVeiculoServiceImpl")
	private TipoVeiculoService tipoVeiculoService;
	
	@SpringBean(name = "veiculoServiceImpl")
	private VeiculoService veiculoService;
	
	private VeiculoGradePanel gridPanel;
	
	private WebMarkupContainer panelCamposTerrestres;
	
	private WebMarkupContainer panelCamposFluviais;

	public ConsultaVeiculosIndexForm(String id) {
		super(id);
		
		panelCamposTerrestres = new WebMarkupContainer("panelCamposTerrestres");
		panelCamposTerrestres.setVisible(false);
		panelCamposTerrestres.setOutputMarkupPlaceholderTag(true);
		
		panelCamposFluviais = new WebMarkupContainer("panelCamposFluviais");
		panelCamposFluviais.setVisible(false);
		panelCamposFluviais.setOutputMarkupPlaceholderTag(true);
		
		FiltroPesquisaVeiculos filtro = 
								new FiltroPesquisaVeiculos(veiculoService);
		
		setDefaultModel(new CompoundPropertyModel<FiltroPesquisaVeiculos>(filtro));
		
		IChoiceRenderer<DominioTipoVeiculo> render = new ChoiceRenderer<>("descricao");
		
		final DropDownChoice<DominioTipoVeiculo> tipoVeiculo = 
							new DropDownChoice<DominioTipoVeiculo>(
							"tipoVeiculo",
							tipoVeiculoService.consultarTodos(),
							render);
		
		tipoVeiculo.add(new AjaxFormComponentUpdatingBehavior("change"){

			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				DominioTipoVeiculo tipo = tipoVeiculo.getModelObject();
				
				boolean isTerrestre = tipo.getCodigo()
											.equals(DominioTipoVeiculo.COD_TERRESTRE);
				
				panelCamposTerrestres.setVisible(isTerrestre);
				panelCamposFluviais.setVisible(!isTerrestre);
				
				target.add(panelCamposTerrestres, panelCamposFluviais);
			}
		});
		
		add(tipoVeiculo);
		
		panelCamposFluviais.add(new TextField<>("nuEmbarcacao"));
		
		panelCamposTerrestres.add(new TextField<>("placa"));
		
		panelCamposTerrestres.add(new TextField<>("rntrc"));
		
		IChoiceRenderer<StatusRegistroAtivo> renderStatus = 
												new ChoiceRenderer<>("descricao");
		
		add(new RadioChoice<>("status",
							  StatusRegistroAtivo.valores(),
							  renderStatus));
		
		gridPanel = new VeiculoGradePanel("panelVeiculos", filtro);
		add(gridPanel);
		
		add(new AjaxButton("pesquisar") {

			private static final long serialVersionUID = 1L;
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {

				target.add(gridPanel);
				
			}
		});
		
		add(new AjaxLink("novo") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				setResponsePage(CadastrarVeiculoIndexPage.class);
			}
			
		});
		
		add(panelCamposTerrestres, panelCamposFluviais);
	}


}
